<template>
    <el-dialog title="转班" :visible.sync="centerDialogVisible" width="500px">
        <el-form :model="form" ref="formRef" :rules="formRules">
            <el-form-item label="班级" prop="teacher_course_id">
                <el-select clearable no-data-text="暂无可转班级" v-model="form.teacher_course_id" placeholder="请选择班级">
                    <el-option v-for="item in list" :key="item.id" :label="item.community_name + '/' + item.teacher_name"
                        :value="item.id">
                    </el-option>
                </el-select>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="centerDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="changeClassRoom">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import { post } from '@/assets/js/http'
export default {
    data() {
        return {
            list: [],
            centerDialogVisible: false,
            order_family_id: '',
            order_id: '',
            loading: false,
            form: {
                teacher_course_id: '',
            },
            formRules:{
                teacher_course_id: [
                    { required: true, message: '请选择班级', trigger: 'change' }
                ]
            }
        }
    },
    methods: {
        open(id, oid) {
            this.centerDialogVisible = true
            this.order_family_id = id
            this.order_id = oid
            this.getList()
        },
        close() {
            this.centerDialogVisible = false
            this.$refs.formRef.resetFields()
        },
        async getList() {
            this.loading = true
            let res = await post({
                url: '/admin/project.Course/shift_course_list',
                data: {
                    order_family_id: this.order_family_id,
                    order_id: this.order_id,
                }
            })
            this.list = res
            this.loading = false
        },
        changeClassRoom() {
            this.$confirm('确认转班?', '提示', {

            }).then(() => {
                this.$refs.formRef.validate(async (valid) => {
                    if (valid) {
                        try {
                            this.$loading()
                            await post({
                                url: '/admin/project.Course/shift_course',
                                data: {
                                    teacher_course_id: this.form.teacher_course_id,
                                    order_id: this.order_id,
                                }
                            })
                            this.$message.success('转班成功')
                            this.$emit('confirm')
                            this.close()
                        }finally{
                            this.$loading().close()
                        }
                    }
                })
            })
        }
    }
}
</script>